/**
 * js在计算浮点数时候，结果可能会不准确。
 * @param a
 * @param b
 */

// 加
export const add = (a, b) => {
  let c
  let d
  let e
  a = a || 0
  b = b || 0
  try {
    c = a.toString().split('.')[1].length
  } catch (f) {
    c = 0
  }
  try {
    d = b.toString().split('.')[1].length
  } catch (f) {
    d = 0
  }
  /* eslint no-sequences:0 */
  /* eslint no-return-assign:0 */
  return (e = 10 ** Math.max(c, d)), (mul(a, e) + mul(b, e)) / e
}

// 减
export const sub = (a, b) => {
  let c
  let d
  let e
  a = a || 0
  b = b || 0
  try {
    c = a.toString().split('.')[1].length
  } catch (f) {
    c = 0
  }
  try {
    d = b.toString().split('.')[1].length
  } catch (f) {
    d = 0
  }
  return (e = 10 ** Math.max(c, d)), (mul(a, e) - mul(b, e)) / e
}

// 乘
export const mul = (a, b) => {
  let c = 0
  a = a || 0
  b = b || 0
  const d = a.toString()
  const e = b.toString()
  try {
    c += d.split('.')[1].length
  } catch (_) {
    // eslint-disable-next-line
  }
  try {
    c += e.split('.')[1].length
  } catch (_) {
    // eslint-disable-next-line
  }
  return (Number(d.replace('.', '')) * Number(e.replace('.', ''))) / 10 ** c
}

// 除
export const div = (a, b) => {
  let c
  let d
  let e = 0
  let f = 0
  a = a || 0
  try {
    e = a.toString().split('.')[1].length
  } catch (_) {
    // eslint-disable-next-line
  }
  try {
    f = b.toString().split('.')[1].length
    // eslint-disable-next-line
  } catch (g) {}
  return (
    (c = Number(a.toString().replace('.', ''))),
    (d = Number(b.toString().replace('.', ''))),
    mul(c / d, 10 ** (f - e))
  )
}

// 是否为数字
export const isNumber = val => {
  const regPos = /^\d+(\.\d+)?$/ // 非负浮点数
  const regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/ // 负浮点数
  return (regPos.test(val) || regNeg.test(val))
}

export const range = (start, end, step = 1) => {
  const arr = []
  for (let i = start; i <= end; i += step) {
    arr.push(i)
  }
  return arr
}
